Skip to content

Conversation

Ratish1
Copy link

@Ratish1 Ratish1 commented Aug 26, 2025

Description

Previously, passing a file path that contained multiple @tool decorated functions resulted in only one tool being loaded/registered. This made file-based tool loading inconsistent with directory/module scanning (which returns all decorated tools). This PR fixes that: the file-path loader now collects all decorated function tools and returns them; the registry is updated to register each tool individually. Unit tests and linters were updated and run locally.

Key Changes

  • Collect all DecoratedFunctionTool objects when loading a .py file and return a list when multiple exist.
  • Normalize loader results and register each AgentTool separately in the registry.
  • Add normalize_loaded_tools helper to src/strands/tools/tools.py.
  • Add unit test test_load_python_tool_path_multiple_function_based verifying multiple decorated tools are discovered when loading a file path.
  • Update callers/registry to iterate loaded tools and call mark_dynamic() + register_tool() per tool.

Related Issues

Closes #612

Documentation PR

N/A

Type of Change

Bug fix

Testing

How have you tested the change? Verify that the changes do not break functionality or introduce warnings in consuming repositories: agents-docs, agents-tools, agents-cli

  • I ran hatch run prepare
  • I ran formatters and linters locally:
    • hatch fmt --formatter → 170 files left unchanged
    • hatch fmt --linter → All checks passed
    • pre-commit run --all-files → Format: Passed; Lint: Passed; Type linting: Passed; Unit Tests: Passed
  • I ran the unit test(s) locally:
    • pytest -q tests/strands/tools/test_loader.py::test_load_python_tool_path_multiple_function_based1 passed in 0.16s
  • I ran hatch run test-integ locally.

Checklist

  • I have read the CONTRIBUTING document
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • I have added an appropriate example to the documentation to outline the feature, or no new docs are needed
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@@ -18,15 +18,17 @@ class ToolLoader:
"""Handles loading of tools from different sources."""

@staticmethod
def load_python_tool(tool_path: str, tool_name: str) -> AgentTool:
def load_python_tool(tool_path: str, tool_name: str) -> Union[AgentTool, List[AgentTool]]:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For backwards compatibility, we need to keep this signature as-is.

Let's add a new method, load_python_tools that does what you're doing here but always turns an array. Then load_python_tool can be updated to reference the new api, returning the first element in the array. Let's then document that load_python_tool is deprecated and emit a warning if it's invoked. We can then remove the method in 2.0

@zastrowm zastrowm self-assigned this Aug 29, 2025
@Ratish1 Ratish1 requested a review from zastrowm August 30, 2025 17:16
@zastrowm
Copy link
Member

zastrowm commented Sep 3, 2025

Apologies, but it looks like there's some conflicts; would you be able to rebase?

@Ratish1
Copy link
Author

Ratish1 commented Sep 3, 2025

Yeah mb, I will do it right away.

ratish added 3 commits September 3, 2025 22:48
…om file path

- Collect all DecoratedFunctionTool objects when loading a .py file and return list when multiple exist
- Normalize loader results and register each AgentTool separately in registry
- Add normalize_loaded_tools helper and test for multiple decorated tools
@Ratish1 Ratish1 force-pushed the fix/load-multiple-tools-from-file-path branch from c154a32 to 1508825 Compare September 3, 2025 17:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] Tools loaded by passing a file path only load the last defined tool in file
2 participants